﻿var http = require('http');
var https = require('https');
/*
http.get("http://www.wl.brjzbs.com/data/getgranarypoint", function (res) {
    console.log("Got response: " + res.statusCode);
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {
        // console.log('BODY: ' + chunk);
        var data = JSON.parse(chunk);
        for (var i = 0; i < data.length; i++) {
            console.log("chunk %d ",i);
            console.log(data[i]);
        }
    });
}).on('error', function (e) {
    console.log("Got error: " + e.message);
});
*/
function GetData(url, cb) {
    http.get(url, function (res) {
    // console.log("Got response: " + res.statusCode);
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {
        // console.log('BODY: ' + chunk);
        var data = JSON.parse(chunk);
        if(cb!=null)
            cb(1,data);
        //for (var i = 0; i < data.length; i++) {
        //    console.log("chunk %d ",i);
        //    console.log(data[i]);
        //}

    });
}).on('error', function (e) {
    console.log("Got error: " + e.message);
    if(cb!=null)
        cb(-1,e.message);
});
}

function print(data)
{
    for (var i = 0; i < data.length; i++) {
        console.log(data[i].host);
        for (var j = 0; j < data[i].granary.length;j++){
            console.log(data[i].granary[j]);
        }

    }
}
function https_get(url, cb) {
    https.get(url, function (res) {
    // console.log("Got response: " + res.statusCode);
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {
        var data = JSON.parse(chunk);
        if(cb!=null)
            cb(1,data);
    });
}).on('error', function (e) {
    console.log("Got error: " + e.message);
    if(cb!=null)
        cb(-1,e.message);
});
}
// GetData('http://www.wl.brjzbs.com/api/granary/granarypoints.json', print);
exports.HttpGetData = GetData;
exports.https_get = https_get;